<template>
  <div>
    <el-form :model="formdata" label-width="120px">
      <el-form-item label="是否转思极坐标">
        <el-radio-group v-model="formdata.transSG">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="文件坐标系">
        <el-select v-model="formdata.coordinate" placeholder="请选择坐标系" style="width: 150px">
          <el-option
            v-for="item in selectOpts.coordOpt"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="子午线经度">
        <el-input v-model="formdata.centerLng" style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="文件编码格式">
        <el-select v-model="formdata.charset" placeholder="请选择编码格式" style="width: 150px">
          <el-option v-for="item in selectOpts.charsetOpt" :key="item" :label="item" :value="item"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-button @click="selectFile">选择地块文件</el-button>
    <el-button @click="uploadFile">上传地块文件并解析</el-button>
    <el-divider></el-divider>
    <el-button @click="draw">新增地块</el-button>
    <el-button @click="edit">编辑地块</el-button>

    <block-panel :show.sync="show" :getTool="getTool" :fileInfo="fileInfo"></block-panel>
  </div>
</template>

<script>
import mixins from '../mixins';
import BlockPanel from './BlockPanel';
export default {
  name: 'BlockTool',
  components: {
    BlockPanel
  },
  props: {
    getTool: {
      type: Function,
      required: true
    },
    mapLoadSuccess: Boolean
  },
  mixins: [mixins],
  data() {
    return {
      show: false,
      selectOpts: {
        coordOpt: [
          { id: 0, label: '默认', value: 'DEFAULT' },
          { id: 1, label: '大地2000坐标系', value: 'CGCS2000' },
          { id: 2, label: '西安80坐标系', value: 'XA80' },
          { id: 3, label: '北京54坐标系', value: 'BJ54' }
        ],
        charsetOpt: ['UTF-8', 'GBK']
      },
      fileInfo: null,
      formdata: { transSG: true, coordinate: 'CGCS2000', centerLng: '120', charset: 'UTF-8' },
      tempOid: 10
    };
  },
  watch: {
    mapLoadSuccess(success) {
      if (success) {
        this.render();
      }
    }
  },
  methods: {
    selectFile() {
      this.getTool('BlockTool').selectFile({
        onChangeFile: (e) => {
          console.log('file changed', e);
          this.$message(`已选择文件${e.target.value}`);
        }
      });
    },
    uploadFile() {
      this.getTool('BlockTool')
        .doUpload(this.formdata)
        .then((fileInfo) => {
          console.log('fileInfo', fileInfo);
          this.fileInfo = fileInfo;
          this.show = true;
        });
    },
    render() {
      this.features.forEach((feature) => {
        feature.properties.data = {
          geometry: feature.geometry
        };
      });
      this.getTool('BlockTool').render(this.features);
    },
    draw() {
      this.getTool('BlockTool').draw({
        onCreate: (e) => {
          console.log('onCreate', e);
        },
        onUpdate: (e) => {
          console.log('onUpdate', e);
        }
      });
    },
    edit() {
      this.getTool('BlockTool').edit(this.features[0], {
        onCreate: (e) => {
          console.log('onCreate', e);
        },
        onUpdate: (e) => {
          console.log('onUpdate', e);
        }
      });
    }
  }
};
</script>

<style scoped></style>
